#ifndef GRAPHGUI_H
#define GRAPHGUI_H

#include "graph.h"
#include "path.h"
#include <QGraphicsScene>
#include <QGraphicsItem>
#include <QGraphicsLineItem>

#include <QWidget>

class VertexGui;
class EdgeGui;

class ToolTipWidget;

class GISGRAPHLIBSHARED_EXPORT GraphGui : public QGraphicsScene
{
public:
	GraphGui(Graph graph, QObject *parent = 0);
	GraphGui(Graph graph, const QRectF &sceneRect, QObject *parent = 0);

	void colorPath(Path p, const QColor &color = Qt::black);
	void colorEdges(QList<Edge> edges, const QColor &color = Qt::black);

	enum {
		VertexType = QGraphicsItem::UserType + 1,
		EdgeType = QGraphicsItem::UserType + 2
	};
	void refresh();
	void vertexMoved(VertexGui *vg);

	Edge edgeFromItem(const EdgeGui *item) const;
	Vertex vertexFromItem(const VertexGui *item) const;

private:
	void drawGraph();

private:
	Graph m_graph;
	QHash<Vertex, VertexGui *> m_vertices;
	QHash<Edge, EdgeGui *> m_edges;
	QPointF m_topLeft;
	QPointF m_bottomRight;
};

class GISGRAPHLIBSHARED_EXPORT VertexGui : public QGraphicsItem
{
public:
	VertexGui(GraphGui *parent);
	Vertex associatedVertex() const;
	int type() const { return GraphGui::VertexType; }
	QRectF boundingRect() const;
	QPainterPath shape() const;
	void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
protected:
	QVariant itemChange(GraphicsItemChange change, const QVariant &value);
	void mousePressEvent(QGraphicsSceneMouseEvent *event);
	void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
	void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
	void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
	void hoverMoveEvent(QGraphicsSceneHoverEvent *event);
private:
	GraphGui *m_parent;
	ToolTipWidget *m_ttWidget;
};

class GISGRAPHLIBSHARED_EXPORT EdgeGui : public QGraphicsItem
{
public:
	EdgeGui(GraphGui *parent);
	Edge associatedEdge() const;
	int type() { return GraphGui::EdgeType; }
	void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
	QRectF boundingRect() const;
	QPainterPath shape() const;
	void setLine(const QLineF &line);
	void setColor(const QColor &color);
protected:
	void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
	void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
	void hoverMoveEvent(QGraphicsSceneHoverEvent *event);
private:
	GraphGui *m_parent;
	QLineF m_line;
	qreal m_penWidth;
	ToolTipWidget *m_ttWidget;
	QColor m_color;
};

class ToolTipWidget : public QWidget
{
	Q_OBJECT
public:
	ToolTipWidget(Vertex v, QWidget *parent = 0);
	ToolTipWidget(Edge e, QWidget *parent = 0);
	void paintEvent(QPaintEvent *event);
private:
	void initialize();

	enum Type {
		VertexType,
		EdgeType
	} type;
	Vertex vertex;
	Edge edge;
};

#endif // GRAPHGUI_H
